/*
 * @Author: viivLgr 
 * @Date: 2018-03-23 15:31:39 
 * @Last Modified by: viivLgr
 * @Last Modified time: 2018-03-25 22:09:24
 */
'use strict';
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _util = require('util/util.js');
var _order = require('service/order-service.js');
var _address = require('service/address-service.js');
var templateProduct = require('./product-list.string');
var templateAddress = require('./address-list.string');
var addressModal = require("./address-modal.js");

var page = {
    data: {
        selectedAddressId: null
    },
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
        this.loadAddressList();
        this.loadProductList();
    },
    bindEvent: function () {
        var _this = this;
         // 地址的删除
         $(document).on('click', '.address-delete', function (e) {
            e.stopPropagation();
            var shippingId = $(this).parents('.address-item').data('id');
            if(window.confirm('确定要删除该地址吗？')){
                _address.del(shippingId, function(res){
                    _util.successTips('删除地址成功');
                    _this.loadAddressList();
                }, function(err){
                    util.errorTips(err);
                });
            }
        });
        // 地址的添加
        $(document).on('click', '.address-add', function () {
            addressModal.show({
                isUpdate: false,
                onSuccess: function(){
                    _this.loadAddressList();
                }
            });
        });
        // 地址的更新
        $(document).on('click', '.address-update', function (e) {
            e.stopPropagation();
            var shippingId = $(this).parents('.address-item').data('id');
            _address.getAddress(shippingId, function(res){
                addressModal.show({
                    isUpdate: true,
                    data: res,
                    onSuccess: function(){
                        _this.loadAddressList();
                    }
                });
            }, function(err){
                _util.errorTips(err);
            });
            
        });
        // 地址的选择
        $(document).on('click', '.address-item', function () {
            var $this = $(this);
            $this.addClass('active')
                .siblings('.address-item').removeClass('active');
            _this.data.selectedAddressId = $this.data('id');
        });

        // 订单的提交
        $(document).on('click', '.order-submit', function () {
            var shippingId = _this.data.selectedAddressId;
            if (shippingId) {
                _order.createOrder({
                    shippingId: shippingId
                }, function (res) {
                    window.location.href = './payment.html?orderNumber=' + res.orderNo;
                }, function (err) {
                    _util.errorTips(err);
                })
            } else {
                _util.errorTips('请选择地址后再提交');
            }
        });
    },
    // 加载地址列表
    loadAddressList: function () {
        var _this = this,
            $AddressCon = $('.address-con');
        $AddressCon.html('<div class="loading"></div>');
        _address.getAddressList(function (res) {
            _this.addressFilter(res);
            var addressHtml = _util.renderHtml(templateAddress, res);
            $AddressCon.html(addressHtml);
        }, function () {
            $AddressCon.html('<p class="error-tip">地址加载失败，请刷新后重试</p>');
        })
    },
    // 处理地址列表中选中状态
    addressFilter: function(data){
        if(this.data.selectedAddressId){
            var selectedAddressIdFlag = false;
            for(var i = 0, len = data.list.length; i < len; i++){
                if(data.list[i].id === this.data.selectedAddressId){
                    data.list[i].isActive = true;
                    selectedAddressIdFlag = true;
                }
            }
            // 如果以前选中的地址遍历之后不存在，则将保存的id设置为null
            if(!selectedAddressIdFlag){
                this.data.selectedAddressId = null
            }
        }
    },
    // 加载商品清单
    loadProductList: function () {
        var _this = this,
            $productCon = $('.product-con');
        $productCon.html('<div class="loading"></div>');
        _order.getProductList(function (res) {
            var productHtml = _util.renderHtml(templateProduct, res);
            $productCon.html(productHtml);
        }, function () {
            $productCon.html('<p class="error-tip">商品清单加载失败，请刷新后重试</p>');
        })
    }
};

$(function () {
    page.init();
});